<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/6 0006
 * Time: 11:26
 */

class GlobalSingleTradeQueryResponse
{
    protected $response;

    public function __construct($response)
    {
        $xml = simplexml_load_string($response);
        if($xml !== false){
            $this->response = $xml;
        }
    }

    /**
     * 请求是否成功。请求成功不代表业务处理成功。
     * T代表成功
     * F代表失败
     * @return bool
     */
    public function isSuccess()
    {
        return !empty($this->response) && $this->response->alipay->is_success == 'T';
    }

    /**
     * 签名值
     * @return null|SimpleXMLElement[]
     */
    public function sign()
    {
        return empty($this->response) ? null : $this->response->alipay->sign;
    }

    public function signType()
    {
        return empty($this->response) ? null : $this->response->alipay->sign_type;
    }

    /**
     * 只有当查询交易失败后，才返回错误代码。
     * @return string
     */
    public function error()
    {
        return empty($this->response) || !isset($this->response->alipay->error) ? '解析结果失败' : $this->response->alipay->error;
    }

    /**
     * 买家支付宝账号。
     * @return null|null
     */
    public function buyerEmail()
    {
        return empty($this->response) ? null : $this->response->response->trade->buyer_email;
    }

    /**
     * 买家支付宝账号对应的支付宝唯一用户号。
     * @return null|string
     */
    public function buyerId()
    {
        return empty($this->response) ? null : $this->response->response->trade->buyer_id;
    }

    /**
     * 交易状态
     * @return null|string
     */
    public function tradeStatus()
    {
        return empty($this->response) ? null : $this->response->response->trade->trade_status;
    }

    /**
     * @return null|string
     */
    public function isTotalFeeAdjust()
    {
        return empty($this->response) ? null : $this->response->response->trade->is_total_fee_adjust;
    }

    /**
     * 支付宝合作商户网站唯一订单号（确保在商户系统中唯一）。
     * @return null|string
     */
    public function getOutTradeNo()
    {
        return (empty($this->response) || empty($this->response->response->trade->out_trade_no))? null : $this->response->response->trade->out_trade_no;
    }

    /**
     * 支付宝根据商户请求，创建订单生成的支付宝交易号。
     * 最短16位，最长64位。
     * @return null|string
     */
    public function getTradeNo()
    {
        return empty($this->response) ? null : $this->response->response->trade->trade_no;
    }

    /**
     * 商品的标题/交易标题/订单标题/订单关键字等。
     * @return null|string
     */
    public function getSubject()
    {
        return empty($this->response) ? null : $this->response->response->trade->subject;
    }

    /**
     * 是否冻结，true 冻结；false 未冻结
     * @return bool|null
     */
    public function getFlagTradeLocked()
    {
        return empty($this->response) ? null : $this->response->response->trade->flag_trade_locked == 1;
    }

    /**
     * 对一笔交易的具体描述信息。如果是多种商品，请将商品描述字符串累加传给body。
     * @return null|string
     */
    public function getBody()
    {
        return (empty($this->response) || empty($this->response->response->trade->body))? null : $this->response->response->trade->body;
    }

    /**
     * 交易创建的时间。
     * 格式为yyyy-MM-dd HH:mm:ss。
     * @return null|string
     */
    public function getGmtCreate()
    {
        return (empty($this->response) || empty($this->response->response->trade->gmt_create))? null : $this->response->response->trade->gmt_create;
    }

    /**
     * 卖家的支付宝账号。
     * @return null|string
     */
    public function getSellerEmail()
    {
        return (empty($this->response) || empty($this->response->response->trade->seller_email))? null : $this->response->response->trade->seller_email;
    }

    /**
     * 卖家支付宝账号对应的支付宝唯一用户号。
    以2088开头的纯16位数字。
     * @return null|string
     */
    public function getSellerId()
    {
        return (empty($this->response) || empty($this->response->response->trade->seller_id))? null : $this->response->response->trade->seller_id;
    }

    /**
     * 交易金额大于0元。担保交易单笔交易金额不能超过100万，精确到小数点后两位。
     * @return null|float
     */
    public function getTotalFee()
    {
        return (empty($this->response) || empty($this->response->response->trade->seller_id))? null : floatval($this->response->response->trade->seller_id);
    }

    /**
     * 单位为：RMB Yuan。取值范围为[0.01,1000000.00]，精确到小数点后两位。
     * @return float|null
     */
    public function getPrice()
    {
        return (empty($this->response) || empty($this->response->response->trade->price))? null : floatval($this->response->response->trade->price);
    }

    /**
     * 购买数量
     * @return int|null
     */
    public function getQuantity()
    {
        return (empty($this->response) || empty($this->response->response->trade->quantity))? null : intval($this->response->response->trade->quantity);
    }

    /**
     * 是否使用了优惠券
     * @return bool
     */
    public function isUseCoupon()
    {
        return !empty($this->response) && !empty($this->response->response->trade->use_coupon) && $this->response->response->trade->use_coupon == 'T';
    }

    /**
     * 优惠折扣
     * @return float|null
     */
    public function getDiscount()
    {
        return (empty($this->response) || empty($this->response->response->trade->discount))? null : floatval($this->response->response->trade->discount);
    }

    /**
     * 交易付款的时间。格式为yyyy-MM-dd HH:mm:ss。
     * @return null|string
     */
    public function getGmtPayment()
    {
        return (empty($this->response) || empty($this->response->response->trade->price))? null : $this->response->response->trade->gmt_payment;
    }

    /**
     * 累计退款金额
     * @return float|null
     */
    public function getToBuyerFee()
    {
        return (empty($this->response) || empty($this->response->response->trade->to_buyer_fee))? null : floatval($this->response->response->trade->to_buyer_fee);
    }

    /**
     * 累计的已打款给卖家的金额。
     * @return float|null
     */
    public function getToSellerFee()
    {
        return (empty($this->response) || empty($this->response->response->trade->to_seller_fee))? null : floatval($this->response->response->trade->to_seller_fee);
    }

    /**
     * 收款类型。取值范围请参见
     * @link https://global.alipay.com/service/app_split_cn/24#QueryPaymentType
     * @return null|string
     */
    public function getPaymentType()
    {
        return (empty($this->response) || empty($this->response->response->trade->payment_type))? null : $this->response->response->trade->payment_type;
    }

    /**
     * 商户网站使用的编码格式，如utf-8、gbk、gb2312等。
     * @return null|string
     */
    public function getInputCharset()
    {
        return (empty($this->response) || empty($this->response->request->_input_charset))? null : $this->response->request->_input_charset;
    }

    /**
     * 接口名称
     * @return null|string
     */
    public function getService()
    {
        return (empty($this->response) || empty($this->response->request->service))? null : $this->response->request->service;
    }

    /**
     * 签约的支付宝账号对应的支付宝唯一用户号。
     * 以2088开头的16位纯数字组成。
     * @return null|string
     */
    public function getPartner()
    {
        return (empty($this->response) || empty($this->response->request->partner))? null : $this->response->request->partner;
    }
}





















